package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.core.dao.database.IShopHairServiceDao;
import com.singfan.core.dao.database.model.ShopHairService;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class ShopHairServiceDaoImpl extends DaoBaseGeneric<ShopHairService> implements IShopHairServiceDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public int create(ShopHairService shopHairService) {
        return insertWithAutoIncrement(null, shopHairService);
    }

    @Override
    public ShopHairService selectShopHairService(int serviceId) {
        return selectByPK(null, serviceId);
    }

    @Override
    public void updateShopHairService(ShopHairService shopHairService) {
        updateByPK(null, shopHairService);
    }

    @Override
    public void deleteShopHairService(int serviceId) {
        deleteByPK(null, serviceId);
    }

    @Override
    public List<ShopHairService> selectShopAllHairService(long shopId) {
        String sql = "SELECT * FROM " + getTableName(shopId) + " WHERE shop_id=? ORDER BY service_type ASC";
        return selectList(shopId, sql, shopId);
    }

    @Override
    public List<ShopHairService> selectShopHairServiceList(long shopId, List<Integer> serviceIdList) {
        List<ShopHairService> result;

        int idCount = serviceIdList.size();
        if (idCount > 0){
            List<Object> paramList = new ArrayList<>();
            paramList.add(shopId);
            String idPlaceHolder ="";
            for (int i=0;i<idCount;i++){
                if (i == 0){
                    idPlaceHolder += "?";
                }
                else {
                    idPlaceHolder += ",?";
                }
                paramList.add(serviceIdList.get(i));
            }
            String sql = "SELECT * FROM " + getTableName(shopId) + " WHERE shop_id=? AND id in("+idPlaceHolder+")";
            result = selectList(null, sql, paramList);
        }
        else {
            result = new ArrayList<>();
        }

        return result;
    }
}
